package cn.iocoder.yudao.module.member.service.job;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.job.vo.*;
import cn.iocoder.yudao.module.member.convert.job.MemberJobConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.MemberCompanyJobDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.MemberJobDO;
import cn.iocoder.yudao.module.member.dal.mysql.company.CompanyMapper;
import cn.iocoder.yudao.module.member.dal.mysql.job.MemberJobMapper;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.api.message.dto.MessageCreateReqDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.dal.redis.common.CountRedisDAO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.Duration;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.JOB_IMPORT_IS_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.JOB_NOT_EXISTS;

/**
 * 招聘职位 Service 实现类
 *
 * @author 谭万全
 */
@Service
@Validated
@Slf4j
public class MemberJobServiceImpl implements MemberJobService {

    @Resource
    private MemberJobMapper memberJobMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private CountRedisDAO countRedisDAO;
    @Resource
    private MessageApi messageApi;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private CompanyService companyService;

    static final String clickBusiness="job_click_count";
    static final String postBusiness="job_post_count";

    private  static final HashMap<String,String> educationMap =new HashMap<String, String>();
    static{
        educationMap.put("大专-大一", "[1,2]");
        educationMap.put("大专-大二", "[1,3]");
        educationMap.put("大专-大三", "[1,4]");
        educationMap.put("本科-大一", "[5,6]");
        educationMap.put("本科-大二", "[5,7]");
        educationMap.put("本科-大三", "[5,8]");
        educationMap.put("本科-大四", "[5,9]");
        educationMap.put("硕士-研一", "[10,11]");
        educationMap.put("硕士-研二", "[10,12]");
        educationMap.put("硕士-研三", "[10,13]");
        educationMap.put("博士", "[14,15]");
    }

    @Override
    public Long createJob(MemberJobCreateReqVO createReqVO) {
        Long tenantId=createReqVO.getTenantId();

        // 插入
        MemberJobDO job = MemberJobConvert.INSTANCE.convert(createReqVO);
        if(SecurityFrameworkUtils.getLoginUser().getUsername().equals("admin")){
            job.setStatus("2"); // 新增职位状态默认为编辑中的草稿状态
            CompanyDO companyDO= companyMapper.selectById(createReqVO.getCompanyId());
            tenantId=companyDO.getTenantId();
        }else {
            job.setStatus("0"); // 新增职位状态默认为编辑中的草稿状态
        }
        job.setCreator(SecurityFrameworkUtils.getLoginUser().getUsername());
        job.setUpdater(SecurityFrameworkUtils.getLoginUser().getUsername());
        job.setOperation(SecurityFrameworkUtils.getLoginUser().getUsername());
        TenantUtils.execute(tenantId, () -> {
            //执行创建企业insert
            memberJobMapper.insert(job);
        });
        // 返回
        return job.getId();
    }

    @Override
    public void updateJob(MemberJobUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateJobExists(updateReqVO.getId());
        // 更新
        MemberJobDO updateObj = MemberJobConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUpdater(SecurityFrameworkUtils.getLoginUser().getUsername());
        updateObj.setOperation(SecurityFrameworkUtils.getLoginUser().getUsername());
        memberJobMapper.updateById(updateObj);
    }

    @Override
    public void updateJob(@Valid MemberJobDO memberJobDO) {
        // 校验存在
        this.validateJobExists(memberJobDO.getId());
        // 更新
        memberJobDO.setOperation(SecurityFrameworkUtils.getLoginUser().getUsername());
        memberJobMapper.updateById(memberJobDO);
    }

    @Override
    public void updateOrderNum(Long id) {
        MemberJobDO jobDO = memberJobMapper.selectById(id);
        if(null==jobDO){
            throw exception(JOB_NOT_EXISTS);
        }
        MemberJobDO updateObj =new MemberJobDO();
        updateObj.setUpdater(WebFrameworkUtils.getLoginUserName());
        updateObj.setUpdateTime(new Date());
        updateObj.setId(id);
        if(null==jobDO.getOrderNum()|| 0==jobDO.getOrderNum()){
            LambdaQueryWrapper<MemberJobDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(MemberJobDO::getOrderNum) // 填入需要查询的字段
                    .orderByDesc(MemberJobDO::getOrderNum) // 按该字段降序排序
                    .last("LIMIT 1"); // 只返回最大值的一条记录
            MemberJobDO result = memberJobMapper.selectOne(queryWrapper);
            if(null==result){
                updateObj.setOrderNum(1);
            }else{
                updateObj.setOrderNum(result.getOrderNum()+1);
            }
        }else{
            updateObj.setOrderNum(0);
        }
        memberJobMapper.updateById(updateObj);
    }

    @Override
    public void setJobsTopStatus(@Valid List<Long> ids,String status) {
        // 更新
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        //将ids转成list集合
        ids.forEach(v->{
            MemberJobDO memberJobDO= new MemberJobDO();
            memberJobDO.setId(v);
            memberJobDO.setOperation(loginUser.getUsername());
            memberJobDO.setTopStatus(status);
            memberJobDO.setUpdater(SecurityFrameworkUtils.getLoginUser().getUsername());
            memberJobMapper.updateById(memberJobDO);
        });

    }
    @Override
    public void setJobStatus(@Valid List<Long> ids) {
        // 更新
        //将ids转成list集合
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        ids.forEach(v->{
            MemberJobDO memberJobDO= new MemberJobDO();
            memberJobDO.setId(v);
            memberJobDO.setOperation(loginUser.getUsername());
            memberJobDO.setStatus("enable");
            memberJobMapper.updateById(memberJobDO);
        });

    }

    @Override
    public void setJobStatus(@Valid List<Long> ids,String status) {
        // 更新
        //将ids转成list集合
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        ids.forEach(v->{
            MemberJobDO memberJobDO= new MemberJobDO();
            memberJobDO.setId(v);
            memberJobDO.setOperation(loginUser.getUsername());
            memberJobDO.setStatus(status);
            memberJobMapper.updateById(memberJobDO);
        });

    }

    @Override
    public Integer addCount(Long id,String type,int sours) {
        Integer newCount=0;
        Duration d = Duration.ofDays(1);
        if(sours==1) {//是否记录
            if (type.equals("post")) {//投递记录
                String count = countRedisDAO.get(postBusiness, id.toString());
                if (StringUtils.isEmpty(count)) {
                    MemberJobDO memberJobDO = memberJobMapper.selectById(id);
                    if (memberJobDO.getPostCount() == null) {
                        countRedisDAO.set(postBusiness, id.toString(), newCount, d);
                    } else {
                        Integer postCount = memberJobDO.getPostCount();
                        newCount = postCount + 1;
                        countRedisDAO.set(postBusiness, id.toString(), newCount, d);
                    }
                } else {
                    Integer postCount = Integer.parseInt(count);
                    newCount = postCount + 1;
                    countRedisDAO.set(postBusiness, id.toString(), newCount, d);
                }
            } else if (type.equals("click")) {//点击记录
                String count = countRedisDAO.get(clickBusiness, id.toString());
                if (StringUtils.isEmpty(count)) {
                    MemberJobDO memberJobDO = memberJobMapper.selectById(id);
                    if (memberJobDO.getClickCount() == null) {
                        countRedisDAO.set(clickBusiness, id.toString(), newCount, d);
                    } else {
                        Integer clickCount = memberJobDO.getClickCount();
                        newCount = clickCount + 1;
                        countRedisDAO.set(clickBusiness, id.toString(), newCount, d);
                    }
                } else {
                    Integer clickCount = Integer.parseInt(count);
                    newCount = clickCount + 1;
                    countRedisDAO.set(clickBusiness, id.toString(), newCount, d);
                }
            }
        }
        return newCount;

    }

    @Override
    public void deleteJob(@Valid List<Long> ids) {
        // 更新
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        //将ids转成list集合
        ids.forEach(v->{
            MemberJobDO memberJobDO= new MemberJobDO();
            memberJobDO.setId(v);
            memberJobDO.setOperation(loginUser.getUsername());
            memberJobDO.setUpdater(loginUser.getUsername());
            memberJobDO.setDeleted(true);
            memberJobDO.setUpdateTime(new Date());
            memberJobMapper.updateById(memberJobDO);
            memberJobMapper.deleteById(v);
        });

    }

    @Override
    public List<MemberJobDO> getAllJobByIds(List<Long> ids) {
        return memberJobMapper.getAllJobByIds(ids);
    }

    @Override
    public void examinJob(MemberJobUpdateReqVO updateReqVO) {
        AdminUserRespDTO loginUser=adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId()) ;
        updateReqVO.setOperation(loginUser.getUserName());
        updateReqVO.setPublishTime(new Date());
        MemberJobDO  memberJobDO=memberJobMapper.selectById(updateReqVO.getId());
        String title;
        String message;
        Map<String, Long> phones=this.getCompanyAdminCreatorPhone(memberJobDO.getId());
        if(updateReqVO.getStatus().equals(2)){
            title=updateReqVO.getJobName()+"：职位审核通过";
            message="您好，创建的职位："+updateReqVO.getJobName()+"  审核成功  创建人："+memberJobDO.getCreator()+"  审核人："+loginUser.getNickname();
            for(String key:phones.keySet()){
                Long id = phones.get(key);
                sendmessage(1l,id,title,message);
            }
            //发送给运营
            title=updateReqVO.getCompanyName()+"-"+updateReqVO.getJobName()+"：职位提交审核-审核通过";
            message="您好，职位："+updateReqVO.getCompanyName()+"-"+updateReqVO.getJobName()+" 审核成功  创建人："+memberJobDO.getCreator()+"  审核人："+loginUser.getNickname();
            List<Long> ids= adminUserApi.getUserIdsByTenant(2l);
            for (Long id1:ids) {
                sendmessage(1l,id1,title,message);
            }
        }else{
            title=updateReqVO.getJobName()+"：职位审核失败";
            message="您好，创建的职位："+updateReqVO.getJobName()+"  审核失败  失败原因为："+updateReqVO.getRemark()+" 创建人："+memberJobDO.getCreator()+"审核人："+loginUser.getNickname();
            for(String key:phones.keySet()){
                Long id = phones.get(key);
                sendmessage(1l,id,title,message);
            }
            title=updateReqVO.getCompanyName()+"-"+updateReqVO.getJobName()+"：审核失败 失败原因为"+updateReqVO.getRemark()+" 创建人："+memberJobDO.getCreator()+"审核人："+loginUser.getNickname();
            message="您好，职位："+updateReqVO.getCompanyName()+"-"+updateReqVO.getJobName()+" 审核成功  创建人："+memberJobDO.getCreator()+"  审核人："+loginUser.getNickname();
            List<Long> ids= adminUserApi.getUserIdsByTenant(2l);
            for (Long id1:ids) {
                sendmessage(1l,id1,title,message);
            }
        }
        this.updateJob(updateReqVO);
    }
    public Map<String,Long> getCompanyAdminCreatorPhone(Long id) {
        MemberJobDO memberJobDO=this.getJob(id,2);
        AdminUserRespDTO creatorUser= adminUserApi.getUser(memberJobDO.getUserId());
        CompanyDO companyDO=companyService.getCompany(memberJobDO.getCompanyId(),2) ;
        AdminUserRespDTO adminUser= adminUserApi.getUserByMobile(companyDO.getPhoneNumber(),memberJobDO.getCompanyId());

        Map<String,Long> phones=new HashMap<>();
        phones.put("creatorUser",creatorUser.getId());
        phones.put("adminUser",adminUser.getId());
        return phones;
    }
    @Async
    void sendmessage(Long type, Long id, String title, String messageText){
        MessageCreateReqDTO messageCreateReqVO=new MessageCreateReqDTO();
        messageCreateReqVO.setPhone("");
        messageCreateReqVO.setType(type);
        messageCreateReqVO.setStatus("5");
        messageCreateReqVO.setUserId(id);
        messageCreateReqVO.setStudentName("系统管理员");
        messageCreateReqVO.setRemark(messageText);
        messageCreateReqVO.setTitle(title);
        messageApi.sendMessage(messageCreateReqVO);
    }
    @Override
    public void deleteJob(Long id) {
        // 校验存在
        this.validateJobExists(id);
        // 删除
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        MemberJobDO memberJobDO= new MemberJobDO();
        memberJobDO.setId(id);
        memberJobDO.setOperation(loginUser.getUsername());
        memberJobDO.setUpdater(loginUser.getUsername());
        memberJobDO.setDeleted(true);
        memberJobDO.setUpdateTime(new Date());
        memberJobMapper.updateById(memberJobDO);
        memberJobMapper.deleteById(id);
    }

    private void validateJobExists(Long id) {
        if (memberJobMapper.selectById(id) == null) {
            throw exception(JOB_NOT_EXISTS);
        }
    }

    @Override
    public MemberJobDO getJob(Long id,int type) {
        MemberJobDO memberJobDO=memberJobMapper.selectById(id);
        if(memberJobDO!=null){
            Integer count=addCount(id,"click",type);
            memberJobDO.setClickCount(count);
            //优化接口,没必要修改全部
           // updateJobCount(memberJobDO);
            memberJobMapper.updateJobClickCount(id,count);
        }

        return memberJobDO;
    }

    @Override
    public MemberJobDO getJob(Long id) {
        MemberJobDO memberJobDO=memberJobMapper.selectById(id);
        return memberJobDO;
    }

    @Override
    public MemberJobDO getJobAll(Long id) {
        MemberJobDO list = memberJobMapper.getJobAll(id);
        return list;
    }

    @Async
    public void updateJobCount(MemberJobDO updateReqVO) {
        memberJobMapper.updateById(updateReqVO);
    }
    @Override
    public List<MemberJobDO> getJobList(Collection<Long> ids) {
        return memberJobMapper.selectBatchIds(ids);
    }

    @Override
    public List<MemberCompanyJobDO> getJobListTop2(Long companyId) {
        return memberJobMapper.getJobListTop2(companyId);
    }

    @Override
    public PageResult<MemberJobDO> getJobPage(MemberJobPageReqVO pageReqVO) {
        return memberJobMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberJobDO> getHotJob(Integer pageSize,List<Long> ids) {
        return memberJobMapper.getHotJob(pageSize,ids);
    }

    @Override
    public List<MemberJobDO> getHotJob() {
        return this.getHotJob(50,null);
    }

    @Override
    public List<MemberJobDO> getJobList(MemberJobExportReqVO exportReqVO) {
        return memberJobMapper.selectList(exportReqVO);
    }


    @Override
    public PageResult<MemberJobDO> getJobWaitExaminePage(MemberJobPageReqVO pageReqVO,List<String> statuss) {
       List<String> educations=new ArrayList<>();
        educations=pageReqVO.getEducations();
        Integer total = memberJobMapper.countJobWaitExaminePage1(pageReqVO,statuss,educations,pageReqVO.getIndustry());
        if (total <= 0) {
            return PageResult.empty();
        }
        List<MemberJobDO> companyUserPage = memberJobMapper.getJobWaitExaminePage1(pageReqVO,statuss,educations,pageReqVO.getIndustry(),null);
        return PageResult.build(companyUserPage, total);
    }

    @Override
    public PageResult<MemberJobDO> getJobPage(MemberJobPageReqVO pageReqVO,List<String> statuss) {
        List<String> educations =null;
        if(pageReqVO.getEducations()!=null){
            educations=pageReqVO.getEducations();
        }
        Integer total = memberJobMapper.countJobWaitExaminePage1(pageReqVO,statuss,educations,pageReqVO.getIndustry());
        if (total <= 0) {
            return PageResult.empty();
        }
        List<MemberJobDO> companyUserPage = memberJobMapper.getJobWaitExaminePage1(pageReqVO,statuss,educations,pageReqVO.getIndustry(),null);
        for (MemberJobDO memberJobDO:companyUserPage){
            CompanyDO companyDO=companyMapper.selectById(memberJobDO.getCompanyId());
            if(companyDO==null||StringUtils.isEmpty(companyDO.getCompanyLogo())||StringUtils.isEmpty(companyDO.getIndustry())){
                continue;
            }
            memberJobDO.setCompanyLogo(companyDO.getCompanyLogo());
            memberJobDO.setIndustry(companyDO.getIndustry());
        }
        return PageResult.build(companyUserPage, total);
    }
    @Override
    public List<MemberJobDO> getJobPage1(MemberJobPageReqVO pageReqVO,List<String> statuss,List<Long> ids) {
        List<String> educations =null;
        if(pageReqVO.getEducations()!=null){
            educations=pageReqVO.getEducations();
        }
        List<MemberJobDO> companyUserPage = memberJobMapper.getJobWaitExaminePage1(pageReqVO,statuss,educations,pageReqVO.getIndustry(),ids);
        if(CollectionUtils.isNotEmpty(companyUserPage)){
            for (MemberJobDO memberJobDO:companyUserPage){
                CompanyDO companyDO=companyMapper.selectById(memberJobDO.getCompanyId());
                if(companyDO==null||StringUtils.isEmpty(companyDO.getCompanyLogo())||StringUtils.isEmpty(companyDO.getIndustry())){
                    continue;
                }
                memberJobDO.setCompanyLogo(companyDO.getCompanyLogo());
                memberJobDO.setIndustry(companyDO.getIndustry());
            }
        }
        return companyUserPage;
    }
    @Override
    public List<MemberJobDO> getJobViewPage(MemberJobPageReqVO pageReqVO ) {
        List<MemberJobDO> companyUserPage = memberJobMapper.getJobView(pageReqVO);
        return companyUserPage;
    }

    @Override
    public JobImportRespVO importJob(List<EnterpriseJobImputReqVO> importJobs, Boolean updateSupport) {
        if (CollUtil.isEmpty(importJobs)) {
            throw exception(JOB_IMPORT_IS_EXISTS);
        }
        LoginUser loginUser=SecurityFrameworkUtils.getLoginUser();
        Long companyId=loginUser.getCompanyId();
        JobImportRespVO respVO = JobImportRespVO.builder().createJobnames(new ArrayList<>())
                .updateJobnames(new ArrayList<>()).failureJobnames(new LinkedHashMap<>()).build();
        importJobs.forEach(importJob -> {
            if(importJob.getCreditCode()!=null&&!importJob.getCreditCode().equals("")){
                if(importJob.getCreditCode()==null||importJob.getCreditCode().equals("")){
                    return;
                }
                CompanyDO companyDO=companyMapper.selectOne("credit_code",importJob.getCreditCode(),"deleted",0);
                if(companyDO==null||companyDO.getCompanyLogo()==null){
                    log.error(importJob.getCreditCode()+":is null");
                    return ;
                }
                MemberJobDO memberJobDO=MemberJobConvert.INSTANCE.convert(importJob);
                String education=importJob.getEducation();
                String city=importJob.getCity();
                memberJobDO.setEducation(covenEducation(education));
                memberJobDO.setCity(covenCity(city));
                memberJobDO.setCompanyId(companyDO.getId());
                memberJobDO.setCompanyName(companyDO.getCompanyName());
                memberJobDO.setCompanyLogo(companyDO.getCompanyLogo());
                memberJobDO.setIndustry(companyDO.getIndustry());
                memberJobDO.setSalaryUnit("daily_salary");
                memberJobDO.setUserId(loginUser.getId());
                memberJobDO.setStatus("0");
                memberJobDO.setPublishTime(new Date());
                memberJobDO.setCreator(loginUser.getUsername());
                memberJobDO.setDepartment(importJob.getDepartment());
                memberJobDO.setWelfareTags(covenWelfareTags(importJob.getWelfareTags()));
                // 判断如果不存在，在进行插入
                TenantUtils.execute(companyDO.getTenantId(), () -> {
                    //执行创建企业insert
                    memberJobMapper.insert(memberJobDO);

                });
                respVO.getUpdateJobnames().add(importJob.getJobName());
            }

        });

        return respVO;
    }
    private static String covenCity(String city){
        return "[\""+city.replace("-","\",\"")+"\"]";
    }

    private static String covenEducation(String educationJSON){
        if(educationJSON.indexOf("，")!=-1){
            educationJSON= educationJSON.replaceAll("，",",");
        }
        String[] educations=educationJSON.split(",");
        StringBuffer str=new StringBuffer("[");
        for (int i=0;i<educations.length;i++){
            if(educations[i]==null){
                continue;
            }
            str.append(educationMap.get(educations[i]));
            if(i!=educations.length-1){
                str.append(",");
            }else{
                str.append("]");
            }
        }
        return str.toString();
    }
    private static String covenWelfareTags(String welfareTagsJSON){
        if(welfareTagsJSON.indexOf("，")!=-1){
            welfareTagsJSON= welfareTagsJSON.replaceAll("，",",");
        }
        String[] welfareTags=welfareTagsJSON.split(",");
        StringBuffer str=new StringBuffer("[");
        for (int i=0;i<welfareTags.length;i++){
            if(welfareTags[i]==null){
                continue;
            }
            str.append("\""+welfareTags[i]+"\"");
            if(i!=welfareTags.length-1){
                str.append(",");
            }else{
                str.append("]");
            }
        }
        return str.toString();
    }

    @Override
    public PageResult<MemberJobDO> getMemberJobPage(MemberJobPageReqVO pageReqVO ) {
        // MyBatis Plus 查询
        IPage<MemberJobDO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        // 转换返回
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(pageReqVO.getCompanyName())){
            params.put("companyName",pageReqVO.getCompanyName());
        }
        if(StringUtils.isNotBlank(pageReqVO.getCity())){
            params.put("city",pageReqVO.getCity());
        }
        if(StringUtils.isNotBlank(pageReqVO.getJobName())){
            params.put("jobName",pageReqVO.getJobName());
        }
        if(StringUtils.isNotBlank(pageReqVO.getOperation())){
            params.put("operation",pageReqVO.getOperation());
        }
        if(StringUtils.isNotBlank(pageReqVO.getUpdater())){
            params.put("updater",pageReqVO.getUpdater());
        }
        mpPage.setRecords(memberJobMapper.getMemberJobPage(mpPage, params));
        return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
    }

    @Override
    public MemberJobDO getRecoveryMemberJobById(Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id",id);
        List<MemberJobDO> list = memberJobMapper.getMemberJobPage(null, params);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void updateBatchRecoveryMemberJob(List<Long> list) {
        memberJobMapper.updateBatchRecoveryMemberJob(list);
    }


    @Override
    public List<MemberJobDO> getRankingJobList(MemberJobPageReqVO request) {
        List<MemberJobDO> rList = memberJobMapper.getRankingJobList(request);
        return rList;
    }

    @Override
    public void updateJobHot(Long id, Integer isHot) {
        memberJobMapper.updateJobHot(id,isHot);
    }

    @Override
    public void updateJobNew(Long id, Integer isNew) {
        memberJobMapper.updateJobNew(id,isNew);
    }

    @Override
    public void updateRankingJobSort(RankingJobIdReqVO rankingIdReqVO) {
        List<Long> ids = rankingIdReqVO.getIds();
        List<MemberJobDO> list=new ArrayList<>();
        int size=ids.size();
        for (int i = 0; i < size; i++) {
            MemberJobDO r=new MemberJobDO();
            r.setId(ids.get(i));
            if(1==rankingIdReqVO.getType()){
                r.setHotWeight(size-i);
            }else if(2==rankingIdReqVO.getType()){
                r.setNewWeight(size-i);
            }
            list.add(r);
        }
        if(1==rankingIdReqVO.getType()){
            memberJobMapper.rankingHotJobSort(list);
        }else if(2==rankingIdReqVO.getType()){
            memberJobMapper.rankingNewJobSort(list);
        }
    }

    @Override
    public void updateJobFixed(Long id, Integer timingStatus, String fixedTime) {
        memberJobMapper.updateJobFixed(id,timingStatus,fixedTime);
    }
}
